import 'dart:async';

import 'package:flutter_common_utils/flutter_common_utils.dart';
import 'package:flutter_live_data/live_data_export.dart';

import '../../flutter_base_framework.dart';

extension LifecycleOwnerExetend on LifecycleOwner {
  static String scopeKey = 'get-scope';

  //获取默认的协程scope
  CoroutineScope getScope() {
    if (extras.containsKey(scopeKey)) {
      return extras[scopeKey] as CoroutineScope;
    }
    final scope = CoroutineScope();
    extras[scopeKey] = scope;
    bindScopeLifecycle(scope);
    return scope;
  }

  // 页面销毁自动取消
  Future<T> withCancel<T>(Future<T> future) {
    return getScope().withCancel(future);
  }

  //创建一个CoroutineScope
  CoroutineScope createScope() {
    final scope = CoroutineScope();
    bindScopeLifecycle(scope);
    return scope;
  }

  //开启一个协程
  Job launchX<T>(Future<T> Function(CoroutineScope) task, [Function? onError]) {
    return getScope().launchS(task, onError);
  }

  //开启一个定时器
  Timer timerPeriodicX<T>(int milliseconds, Function(CoroutineScope, Timer) task, [Function? onError]) {
    return getScope().timerPeriodic(milliseconds, task, onError);
  }

  //开启一个协程
  Job launchXSJ<T>(Future<T> Function(CoroutineScope, Job) task, [Function? onError]) {
    return getScope().launch(task, onError);
  }

  //开启一个协程
  Job launchXJ<T>(Future<T> Function(Job) task, [Function? onError]) {
    return getScope().launchJ(task, onError);
  }

  // 监听生命周期
  void bindJobLifecycle(Job job) {
    final obs = _LifecycleObserver(owner: this, job: job);
    //监听生命周期
    addObserver(obs);
    job.future.whenComplete(() {
      removeObserver(obs);
    });
  }

  // 监听生命周期
  void bindScopeLifecycle(CoroutineScope scope) {
    final obs = _LifecycleObserver(owner: this, scope: scope);
    //监听生命周期
    addObserver(obs);
    // job.future.whenComplete(() {
    //   removeObserver(obs);
    // });
  }
}

//扩展BaseViewModel
extension BaseViewModelExtend on BaseViewModel {
  //创建一个CoroutineScope
  CoroutineScope createScope() => (requireLife as BaseWidgetState).createScope();

  //获取默认的协程scope
  CoroutineScope getScope() => (requireLife as BaseWidgetState).getScope();

  // 页面销毁自动取消
  Future<T> withCancel<T>(Future<T> future) {
    return getScope().withCancel(future);
  }

  //开启一个协程
  Job launchX<T>(Future<T> Function(CoroutineScope) task, [Function? onError]) => (requireLife as BaseWidgetState).launchX(task, onError);

  Timer timerPeriodicX<T>(int milliseconds, Function(CoroutineScope, Timer) task, [Function? onError]) => (requireLife as BaseWidgetState).timerPeriodicX(milliseconds, task, onError);

  //开启一个协程
  Job launchXSJ<T>(Future<T> Function(CoroutineScope, Job) task, [Function? onError]) => (requireLife as BaseWidgetState).launchXSJ(task, onError);

  //开启一个协程
  Job launchXJ<T>(Future<T> Function(Job) task, [Function? onError]) => (requireLife as BaseWidgetState).launchXJ(task, onError);

  // 监听生命周期
  void bindScopeLifecycle(CoroutineScope scope) => (requireLife as BaseWidgetState).bindScopeLifecycle(scope);

  // 监听生命周期
  void bindJobLifecycle(Job job) => (requireLife as BaseWidgetState).bindJobLifecycle(job);
}

// 协程绑定生命周期
extension CoroutineScopeExtend on CoroutineScope {
  //与生命周期绑定
  CoroutineScope bindLifecycle(LifecycleOwner owner) {
    final obs = _LifecycleObserver(owner: owner, scope: this);
    //监听生命周期
    owner.addObserver(obs);
    return this;
  }
}

class _LifecycleObserver extends LifecycleObserver {
  _LifecycleObserver({
    required this.owner,
    this.scope,
    this.job,
  });

  final LifecycleOwner owner;
  final CoroutineScope? scope;
  final Job? job;

  @override
  void onDispose() {
    super.onDispose();
    owner.removeObserver(this);
    scope?.cancel();
    job?.cancel();
  }
}
